/*


TODO: documentation, vertical scrolling
*/

(function($) {

$.fn.carousel = function(options) {
	var settings = $.extend({
		prepend: 'carousel_'
	}, options || {});
	var return_false = function() { return false; }
	var $body = $(document.body);
	var prepend = settings.prepend;

	return $(this).each(function() {
		var $this = $(this);
		var mouse_start_x;
		var obj_start_x;
		var total_moved = 0;
		var delta_x;
		var last_x;

		var drag = function(event) {
			if ( event.originalEvent.touches ) {
				event.pageX = event.originalEvent.touches[0].clientX;
				event.pageY = event.originalEvent.touches[0].clientY;
			}

			$this.stop(true, true);
			delta_x = event.pageX - last_x;
			$this.attr({
				scrollLeft: obj_start_x - (event.pageX - mouse_start_x)
				});

			total_moved += Math.abs(event.pageX - mouse_start_x);
			last_x = event.pageX;
		};

		var stop = function(event) {
			$body
				.unbind('mousemove touchmove', drag)
				.unbind('mouseup mouseleave touchup touchleave', stop);

			$this.animate({scrollLeft: $this.attr('scrollLeft') - delta_x * 7}, {queue: false, duration: 800, easing: 'cubicEaseOut'});

			$this.removeClass(prepend + 'mousedown');

			$body.css({MozUserSelect: ''});

			if ($.browser.msie) {
				$body
					.unbind('dragstart', return_false)
					.unbind('selectstart', return_false);
			}
		};

		$this.addClass(prepend + 'javascript');

		$this
			.bind('mousedown touchstart', function(event) {
				if ( event.originalEvent.touches ) {
					event.pageX = event.originalEvent.touches[0].clientX;
					event.pageY = event.originalEvent.touches[0].clientY;
				}

				var x_scroll = ($this.attr('scrollWidth') > $this.outerWidth());
				var y_scroll = ($this.attr('scrollHeight') > $this.outerHeight());

				if ( !x_scroll && !y_scroll ) {
					return;
				}

				if ( x_scroll && event.pageY > $this.offset().top + $this.outerHeight() - $.scrollbarWidth() ) {
					return;
				}

				if ( y_scroll && event.pageX > $this.offset().left + $this.outerWidth() - $.scrollbarWidth() ) {
					return;
				}

				last_x = event.pageX;

				$this.addClass(prepend + 'mousedown');
				event.preventDefault();

				mouse_start_x = event.pageX;
				mouse_start_y = event.pageY;

				obj_start_x = parseInt($this.attr('scrollLeft'));

				var old_moz_user_select = $body.css('MozUserSelect');
				$body.css({MozUserSelect: 'none'});

				$body
					.bind('mousemove touchmove', drag)
					.bind('mouseup mouseleave touchup touchleave', stop);

				if ($.browser.msie) {
					$body
						.bind('dragstart', return_false)
						.bind('selectstart', return_false);
				}
			})
			.bind('click', function() {
				if ( total_moved > 10 ) {
					total_moved = 0;
					return false;
				}
				total_moved = 0;
			});
	});
};

// jQuery adapted Penner animation
//    created by Jamie Lemon
$.extend( $.easing, {
	cubicEaseOut: function(p, n, firstNum, diff) {
		var c = firstNum + diff;
		return c*((p=p/1-1)*p*p + 1) + firstNum;
	}
});

})(jQuery);